package cn.zmit.kuxi.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import cn.zmit.kuxi.R;
import cn.zmit.kuxi.contants.SPConstants;
import cn.zmit.kuxi.contants.Url;
import cn.zmit.kuxi.entity.RecharEntity;
import cn.zmit.kuxi.image.ImageDisplayer;
import cn.zmit.kuxi.request.RequestTask;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.xdroid.common.utils.PreferenceHelper;
import com.xdroid.functions.request.JsonData;
import com.xdroid.request.extension.XDroidRequest;
import com.xdroid.request.extension.config.DataType;
import com.xdroid.request.extension.impl.OnRequestListenerAdapter;

/** * @author @parameter * @return */
public class RechargeRecordActivity extends BaseActivity {
	@ViewInject(R.id.bt_recharge)
	private Button bt_recharge;
	@ViewInject(R.id.imag_user_pic)
	private ImageView imag_user_pic;
	@ViewInject(R.id.tv_my_user_name)
	private TextView tv_my_user_name;

	@ViewInject(R.id.tv_kubi_money)
	private TextView tv_kubi_money;

	@ViewInject(R.id.tv_my_user_phone_num)
	private TextView tv_my_user_phone_num;
	String pic, name, kubi, phone;

	@ViewInject(R.id.listView_recharg_record)
	private ListView listView_recharg_record;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setTitleView("充值记录", true);
		setContentView(R.layout.activity_recharge_record);
		ViewUtils.inject(this);
		getRechargeList();
		initView();
	}

	private void getRechargeList() {
		// get userid and post to server
		hasLog = PreferenceHelper.readBoolean(context,
				SPConstants.SHARED_PREFERENCE_NAME, SPConstants.HAS_LOGGED,
				false);
		if (hasLog) {
			userid = PreferenceHelper.readString(context,
					SPConstants.SHARED_PREFERENCE_NAME, SPConstants.USERID, "");
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("user_id", userid);
			RequestTask.getInstance().doRechargeRecord(this, map,
					new OnRequestListener());
		} else {
			// 跳转登录
		}
	}

	private JsonData data;
	private List<RecharEntity> mRecharList = new ArrayList<RecharEntity>();
	private String kubi_server;
	private class OnRequestListener extends OnRequestListenerAdapter<Object> {

		

		@Override
		public void onDone(XDroidRequest<?> request, String response,
				DataType dataType) {
			super.onDone(request, response, dataType);
			mRecharList.clear();
			JsonData jsonData = JsonData.create(response);
			data = jsonData.optJson("recharge_record");
			kubi_server = jsonData.optString("credit");
			if (data != null && data.length() > 0) {
				for (int i = 0; i < data.length(); i++) {
					JsonData itemData = data.optJson(i);
					String amount = itemData.optString("amount");
					String tranferOn = itemData.optString("transferOn");
					RecharEntity entity = new RecharEntity(amount, tranferOn);
					mRecharList.add(entity);
				}
			}
			//consider thread,setAdapter after get all the data
			listView_recharg_record.setAdapter(new RechargeAdapter());
			tv_kubi_money.setText("酷币：" + kubi_server);
		}
	}

	public class RechargeAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return mRecharList.size();
		}

		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup arg2) {
			View view;
			ViewHolder holder;
			if (convertView != null) {
				view = convertView;
				holder = (ViewHolder) view.getTag();
			} else {
				view = View.inflate(RechargeRecordActivity.this,
						R.layout.item_recharge_record, null);
				holder = new ViewHolder();
				holder.tv_amount = (TextView) view.findViewById(R.id.tv_amount);
				holder.tv_transferOn = (TextView) view
						.findViewById(R.id.tv_transferon);
				view.setTag(holder);
			}
			holder.tv_amount.setText("充值金额："
					+ mRecharList.get(position).getAmount());
			holder.tv_transferOn.setText(mRecharList.get(position)
					.getTranferOn());
			Log.e("amount===", mRecharList.get(position).getAmount());
			Log.e("time===", mRecharList.get(position).getTranferOn());
			return view;
		}
	}

	Boolean hasLog;

	static class ViewHolder {
		TextView tv_amount;
		TextView tv_transferOn;
	}

	private void initView() {
		pic = getTextFromBundle("pic");
		name = getTextFromBundle("name");
		kubi = getTextFromBundle("kubi");
		phone = getTextFromBundle("phone");
		if (pic != null && pic.length() > 0) {
			ImageDisplayer.getInstance().dispalyImageWithRadius(
					Url.IMAGR_ROOT + pic, imag_user_pic, 0);
		} else {
			imag_user_pic.setImageResource(R.drawable.user_pic_def);
		}
		tv_my_user_name.setText("昵称：" + name);
	
		tv_my_user_phone_num.setText("手机号：" + phone);

	}

	@OnClick({ R.id.bt_recharge })
	private void OnClick(View v) {
		switch (v.getId()) {
		case R.id.bt_recharge:
			Bundle bundle = new Bundle();
			bundle.putString("name", name);
			bundle.putString("pic", pic);
			bundle.putString("kubi", kubi);
			bundle.putString("phone", phone);
			openActivity(RechargeActivity.class, bundle);
			break;

		default:
			break;
		}
	}
	@Override
	protected void onStart() {
		getRechargeList();
		super.onStart();
	}
}
